package com.ruoyi.project.jiaowu.kaikeplan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TBKaiKePlanResult;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TBKaikePlanParam;
import com.ruoyi.project.jiaowu.kaikeplan.domain.TbKaikePlan;
import com.ruoyi.project.jiaowu.kaikeplan.domain.UserPlan;
import com.ruoyi.project.jiaowu.kaikeplan.service.ITbKaikePlanService;
import com.ruoyi.project.jiaowu.totalcoursemain.domain.TbTotalCourseMain;
import com.ruoyi.project.jiaowu.totalcoursemain.service.ITbTotalCourseMainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 开课计划Controller
 * 
 * @author ruoyi
 * @date 2019-10-23
 */
@Controller
@RequestMapping("/website/kaikeplan")
public class TbKaikePlanController extends BaseController
{
    private String prefix = "system/jiaowukaikeplan";

    @Autowired
    private ITbKaikePlanService tbKaikePlanService;
    @Autowired
    private ITbTotalCourseMainService tbTotalCourseMainService;

    @RequiresPermissions("website:kaikeplan:view")
    @GetMapping()
    public String plan()
    {
        return prefix + "/plan";
    }

    /**
     * 查询开课计划列表
     */
    @RequiresPermissions("website:kaikeplan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBKaikePlanParam tbKaikePlanParam)
    {
        tbKaikePlanParam.setSign("plan");
        startPage();
        List<TBKaiKePlanResult> list = tbKaikePlanService.selectTbKaikePlanList(tbKaikePlanParam);
        System.err.println(list);
        return getDataTable(list);
    }

    /**
     * 导出开课计划列表
     */
    @RequiresPermissions("website:kaikeplan:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TBKaikePlanParam tbKaikePlanParam)
    {
        List<TBKaiKePlanResult> list = tbKaikePlanService.selectTbKaikePlanList(tbKaikePlanParam);
        ExcelUtil<TBKaiKePlanResult> util = new ExcelUtil<TBKaiKePlanResult>(TBKaiKePlanResult.class);
        return util.exportExcel(list, "plan");
    }

    /**
     * 新增开课计划
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TbTotalCourseMain> list = tbTotalCourseMainService.selectTbTotalCourseMainList(null);
        mmap.put("list",list);
        return prefix + "/add";
    }

    /**
     * 新增保存开课计划
     */
    @RequiresPermissions("website:kaikeplan:add")
    @Log(title = "开课计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbKaikePlan tbKaikePlan)
    {
        UserPlan userPlan= new UserPlan();
        userPlan.setUserId(getSysUser().getUserId());
        userPlan.setUserStates(Integer.valueOf(getSysUser().getStatus()));
        return toAjax(tbKaikePlanService.insertTbKaikePlan(tbKaikePlan,userPlan));
    }

    /**
     * 修改开课计划
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbKaikePlan tbKaikePlan = tbKaikePlanService.selectTbKaikePlanById(id);
        mmap.put("tbKaikePlan", tbKaikePlan);
        List<TbTotalCourseMain> list = tbTotalCourseMainService.selectTbTotalCourseMainList(null);
        mmap.put("list",list);
        return prefix + "/edit";
    }

    /**
     * 修改保存开课计划
     */
    @RequiresPermissions("website:kaikeplan:edit")
    @Log(title = "开课计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbKaikePlan tbKaikePlan)
    {
        return toAjax(tbKaikePlanService.updateTbKaikePlan(tbKaikePlan));
    }

    /**
     * 删除开课计划
     */
    @RequiresPermissions("website:kaikeplan:remove")
    @Log(title = "开课计划", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbKaikePlanService.deleteTbKaikePlanByIds(ids));
    }


    /**
     * 发布开课计划
     */
    @RequiresPermissions("system:plan:fabu")
    @Log(title = "发布开课计划", businessType =BusinessType.UPDATE)
    @PostMapping("/fabu")
    @ResponseBody
    public AjaxResult fabu(String id)
    {
        return toAjax(tbKaikePlanService.fabu(id));
    }

    /**
     * 导入开课计划
     */
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importCourse(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TbKaikePlan> util = new ExcelUtil<TbKaikePlan>(TbKaikePlan.class);
        List<TbKaikePlan> tbKaikePlans = util.importExcel(file.getInputStream());
        String message = tbKaikePlanService.importTbKaikePlan(tbKaikePlans, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板下载
     * @return
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TbKaikePlan> util = new ExcelUtil<TbKaikePlan>(TbKaikePlan.class);
        return util.importTemplateExcel("开课计划模板");
    }
}
